////////////////////////////////////////////////////////////////////////////////
//
//	This file is part of Woozox, see <http://code.google.com/p/woozox/>.
//
//	Copyright 2008-2009 Florian David
//
//	Woozoo is free software: you can redistribute it and/or modify
//	it under the terms of the GNU Lesser General Public License as 
//	published by the Free Software Foundation, either version 3 of 
//	the License, or (at your option) any later version.
//
//	Woozoo is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU Lesser General Public 
//	License along with Woozoo. If not, see <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
package org.woozoo.xhtml.core.util
{
	/**
	 * @private
	 * */
	public class StringUtil
	{
		/**
		 * 
	     * This method strips the html tags from a String and returns the results.
	     * The second param allows for certain tags not to be stripped from the string
	     * and are formatted as a String: "<p><b><br />"
	     *
	     * @param str The String to strip it's tags
	     * @param sllowedTags A string of tags allowed
	     * @return A new string with the tags stripped
	     */
		public static function stripTags(str : String, allowedTags : String = null) : String
	    {
	    	if (!str) return "";
	        var stripPattern:RegExp = /<[^>]*>/gim;
	        
	        if (allowedTags != null)
	        {
	            var chars:String = allowedTags.replace(/<>/, "");
	            stripPattern = new RegExp("<[^>"+ chars + "]*>", "gim");
	        }
	        
	        str = str.replace( stripPattern, "" );
	        
	        return str;
	    }
	    
	    /**
	     * 
	     * */
	    public static function strip(str : String) : String
		{
			return str.replace(/^\s*(.*?)\s*$/g, "$1");
		}
		
		/**
		 * adaptation from http://www.macsims.com/blog/?p=17
		 * */
		public static function stripWhitespace(str : String, options : String = "") : String {
			// default is to strip all
		    var result : String = str;
		    // convert tabs to spaces
		    if(options == "" || options.indexOf("t",0) > -1)
		        result = result.split("\t").join(" ");
		    // convert returns to spaces
		    if(options == "" || options.indexOf("r",0) > -1)
		        result = result.split("\r").join(" ");
		    // convert newlines to spaces
		    if(options == "" || options.indexOf("n",0) > -1)
		        result = result.split("\n").join(" ");
		    // compress spaces
		    if(options == "" || options.indexOf("s",0) > -1) {
		        var list : Array = result.split(" ");
		        for (var i : int = list.length - 1; i >= 0; i--) {
		        	var item : String = list[i]; 
		        	if (item.length == 0) {
		         	   list.splice(i, 1);
		         	}
		        }
		        result = list.join(" ");
		    }
		    return result;
		} 
	}
}